<?php

namespace App\Admin\Controllers;

use App;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Models\ParamConfig;
use ParamConfig as ConfigService;
use Excel;

class ParamConfigController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.param_config.title.list'));
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('backend.param_config.title.edit'));
            $content->body($this->form()->edit($id));
        });
    }



    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.param_config.title.add'));
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(ParamConfig::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('ID')->sortable();
            $grid->key(trans('backend.param_config.column.key'));
            $grid->value(trans('backend.param_config.column.value'));
            $grid->tag(trans('backend.param_config.column.tag'));
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->useModal();
                $filter->like('key', trans('backend.param_config.column.key'));
                $filter->like('tag', trans('backend.param_config.column.tag'));
            });
            $grid->disableExport();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ParamConfig::class, function (Form $form) {
            $form->text('key', trans('backend.param_config.column.key'))->rules('required');
            $form->text('tag', trans('backend.param_config.column.tag'))->rules('required');
            $form->editor('value', trans('backend.param_config.column.value'))->attribute('rows', 20)->rules('required');
        });

    }


    public function importRecipeTestingConfig()
    {
        $filePath = 'public/upload/testing/recipe/options_level.xlsx';
        Excel::load($filePath, function ($reader){
            $allData = $reader->all();
            $datas = $allData[0];
            $insertData = [];
            foreach ($datas as $d ){
                $obj = [];
                $key = $d['answer'].'|'.$d['index'].'|'.$d['weight'];
                $obj['key'] = $key;
                $tag = 'RT.level.'.$d['level'];
                $obj['tag'] = $tag;
                $obj['value'] = $d['tag'];
                array_push($insertData,$obj);
            }

            ConfigService::batchInsertData($insertData);
            echo json_encode($insertData);
        });
    }

    public function options()
    {
        return ParamConfigService::getResultOptions();
    }
}
